#! /usr/bin/env python
import inputbox
import pygame
import os
import wx

class Munchkin (pygame.sprite.Sprite):
    def __init__(self, player):
        pygame.sprite.Sprite.__init__(self)

        self.x                   = 0
        self.y                   = 0
        self.player              = player # Set up who controls this munchkin.
        self.player_playfield    = None
        # Players start at level 1.
        self.character_level     = 1
        # Classes:
        # 0 - None, 1 - Cleric, 2 - Thief, 3 - Warrior, 4 - Wizard
        # Players start with no class.
        self.character_class     = 0
        self.character_class2    = 0
        # Races:
        # 0 - None, 1 - Dwarf, 2 - Elf, 3 - Halfling, 4 - Human
        # Players always start out as a human.
        self.character_race      = 4
        self.character_race2     = 0
        # ECL - Effective character level.
        # Characters can gain temporary levels in combat.
        self.character_ecl       = 0
        self.character_temp_ecl  = 0
        self.character_halfbreed = False
        self.character_supermunchkin = False
        self.sex                 = None
        self.changed_sex         = False
        self.image               = pygame.image.load (os.path.join (
            'data/images', 'munchkin_level_counter_1.jpg')).convert()
        self.image = pygame.transform.scale (self.image, (150, 100))
        self.rect                = self.image.get_rect ()
        self.rect.topleft        = (self.x, self.y)
        self.bottomx             = self.x + self.image.get_width()
        self.bottomy             = self.y + self.image.get_height()
        self.click               = False
        self.max_hand_size       = 5
        self.player_turn         = False
        self.combat_active       = False
        self.active_player       = False
        self.master_container    = None
        self.screen              = None
        # To run away in combat, a 5 or 6 is needed on a d6.
        # Elves can run on a 4, 5, or 6
        # Halflings need a 6 to run away
        self.to_run_away           = [5,6]
        # Players can only use 1 big item at a time unless a dwarf
        # or playing a cheat card.
        self.big_item            = []
        self.max_big_items       = 1

        self.SetCoords ()

    def AddCard (self, card):
        # Add a card to this player's playfield.
        self.player_playfield.append (card)

        # Update location of card
        card.location = 2

        # Update card x/y coordinates by updating play field container.
        self.UpdatePlayfieldPositions ()

        # What card was added?
        if card.type == 'race':
            # Race card added to play. Update the player's race as needed.
            self.UpdateRaces ()

    def CheckForHalfbreed (self):
        return self.character_halfbreed

    def CheckRaceOne (self):
        return self.character_race

    def CheckRaceOneTitle (self):
        li = {0:'None',1:'Dwarf',2:'Elf',3:'Halfling',4:'Human'}
        a = self.character_race
        return li[a]

    def CheckRaceTwoTitle (self):
        li = {0:'None',1:'Dwarf',2:'Elf',3:'Halfling',4:'Human'}
        a = self.character_race2
        return li[a]

    def CheckRaceTwo (self):
        return self.character_race2

    def CheckNumRaces (self):
        count = 0
        a = self.CheckRaceOne ()
        b = self.CheckRaceTwo ()
        if a == 0 or a == 4:
            pass
        else:
            count = count + 1
        if b == 0 or b == 4:
            pass
        else:
            count = count + 1
        return count

    def CheckForSuperMunchkin (self):
        return self.character_supermunchkin

    def CheckClassOne (self):
        return self.character_class

    def CheckClassTwo (self):
        return self.character_class2

    def CheckSex (self):
        return self.sex

    def PickRaceCardToDiscard (self):
        # Is user multi-raced?
        if self.CheckForHalfbreed():
            # Half-breed, 2 races possible
            r1 = self.CheckRaceOne
            r2 = self.CheckRaceTwo

            # Translate the codes back into the label.
            rDict = {0: '0-No Race', 1: '1-Dwarf', 2: '2-Elf', 3: '3-Halfling', 4: '4-Human'}

            # Print to user the list of races and have him choose 1.
            delete_card = None
            while delete_card == None:
##                answer = inputbox.ask (self.screen, "Which race to get rid of? " + rDict(r1) +
##                                       " or " + rDict(r2) + "? ")
                # Test code that displays instead to console
                print 'Which race card do you wish to remove? %s or %s' % (r1, r2)
                answer = raw_input (": ")
                if answer == r1:
                    delete_card = r1
                elif answer == r2:
                    delete_card = r2
            if delete_card == 1:
                for count in range(len(self.player_playfield)):
                    if self.player_playfield[count].title == 'Dwarf':
                        delete_card = self.player_playfield[count]
                        break
            elif delete_card == 2:
                for count in range(len(self.player_playfield)):
                    if self.player_playfield[count].title == 'Elf':
                        delete_card = self.player_playfield[count]
                        break
            elif delete_card == 3:
                for count in range(len(self.player_playfield)):
                    if self.player_playfield[count].title == 'Halfling':
                        delete_card = self.player_playfield[count]
                        break
            self.RacePlayfieldToDiscard (delete_card)

        else:
            # Non half-breed. Only 1 race possible.
            card = None
            for count in range(len(self.player_playfield)):
                if self.player_playfield[count].type == 'race':
                    card = self.player_playfield[count]
                    break
            if card != None:
                self.RacePlayfieldToDiscard (card) # Discard the card.

    def PrintData (self):
        # This is a TESTING function only. It prints ONLY to the console.  It does nothing else.
        print '''
------------------------------------------------------------------------
Player            = %s
Location          = %s , %s
Gender            = %s
Gender Changed    = %s
Player Level      = %s
ECL               = %s
Character Classes (0=None, 1=Cleric, 2=Thief, 3=Warrior, 4=Wizard)
Character Class1  = %s
Character Class2  = %s
Supermunchkin     = %s
Character Races (0=None, 1=Dwarf, 2=Elf, 3=Halfling, 4=Human)
Character Race1   = %s
Character Race2   = %s
Character Halfbreed = %s
Max Hand Size     = %s
------------------------------------------------------------------------\n
        ''' % (self.player, self.x, self.y, self.sex, self.changed_sex, self.character_level,
               self.character_ecl, self.character_class, self.character_class2, self.character_supermunchkin,
               self.character_race, self.character_race2, self.character_halfbreed, self.max_hand_size)

    def RemoveCard (self, card):
        # Remove a card from this player's playfield.
        self.player_playfield.pop (self.player_playfield.index (card))
        card.SetLocation (0) # Set the location of the card to 0 for now, as it is no longer here.
        # Update the cards still in play position so that they render correctly again.
        self.UpdatePlayfieldPositions ()

    def RacePlayfieldToDiscard (self, card):
        # This function specifically takes a card and removes it from the playfield.
        # Then it updates the players race. Finally, it adds the card to the discard deck.
        self.RemoveCard (card) #Removes the card from the playfield container.
        # Add card to the discard deck.
        self.master_container[1].AddCard (card)
        self.UpdateRaces () # Update this players races.

    def Show (self):
        # Draw a blue circle for sex if male, pink if female. Then draw this card.
        if self.sex == 'm':
            pygame.draw.circle (self.image, (0, 0, 153), (113, 13), 9, 3)
        else:
            pygame.draw.circle (self.image, (255, 102, 255), (135, 13), 9, 3)
        # Draw a circle over the correct level of the munchkin.
        if self.character_level   == 1:
            pygame.draw.circle (self.image, (204, 0, 0), (84, 82), 9, 3)
        elif self.character_level == 2:
            pygame.draw.circle (self.image, (204, 0, 0), (106, 82), 9, 3)
        elif self.character_level == 3:
            pygame.draw.circle (self.image, (204, 0, 0), (128, 82), 9, 3)
        elif self.character_level == 4:
            pygame.draw.circle (self.image, (204, 0, 0), (95, 66), 9, 3)
        elif self.character_level == 5:
            pygame.draw.circle (self.image, (204, 0, 0), (117, 66), 9, 3)
        elif self.character_level == 6:
            pygame.draw.circle (self.image, (204, 0, 0), (84, 49), 9, 3)
        elif self.character_level == 7:
            pygame.draw.circle (self.image, (204, 0, 0), (106, 49), 9, 3)
        elif self.character_level == 8:
            pygame.draw.circle (self.image, (204, 0, 0), (128, 49), 9, 3)
        elif self.character_level == 9:
            pygame.draw.circle (self.image, (204, 0, 0), (95, 32), 9, 3)
        self.screen.blit (self.image, self.rect)

    def SetCoords (self):
        self.y = (self.player-1) * 170
        self.rect.topleft = (self.x, self.y)
        self.bottomx = self.x + self.image.get_width()
        self.bottomy = self.y + self.image.get_height()

    def SetScreen (self):
        self.screen = self.master_container[22]

    def Update (self, mouse_x, mouse_y, clicked):
        self.click = clicked
        self.mouse_x, self.mouse_y = mouse_x, mouse_y

        if (mouse_x > self.x and mouse_x < self.bottomx and
            mouse_y > self.y and mouse_y < self.bottomy):
            if clicked:
                self.PrintData ()

        for count in range(len(self.player_playfield)):
            self.player_playfield[count].Update (mouse_x, mouse_y, clicked)

    def UpdateMasterContainer (self, master_container):
        self.master_container = master_container
        self.SetScreen ()

    def UpdatePlayfieldPositions (self):
        for count in range(len(self.player_playfield)):
            # Update x, y coordinates.
            # Cards start at x location 230.  Y starts at 0 for player 1, and y is at every 170 pixels.
            x = count*80 + 230
            y = (self.player-1) * 170
            self.player_playfield[count].SetRectTopLeft ((x, y))

    def UpdateRacialAbilities (self):
        # Need to figure out what race combination the player is. Possible combinations:
        # 1 Human
        # 2 Elf
        # 3 Elf with Halfbreed card
        # 4 Dwarf
        # 5 Dwarf with Halfbreed card
        # 6 Halfling
        # 7 Halfling with Halfbreed card
        # 8 Elf / Dwarf
        # 9 Elf Halfling
        # 10 Dwarf / Halfling

    def UpdateRaces (self):
        # Set races to defaults then update as needed.
        self.character_race  = 4
        self.character_race2 = 0

        for card in self.player_playfield:
            if card.type == 'race':
                if card.title == 'Dwarf':
                    if self.character_race == 4:
                        self.character_race = 1
                    else:
                        self.character_race2 = 1
                elif card.title == 'Elf':
                    if self.character_race == 4:
                        self.character_race = 2
                    else:
                        self.character_race2 = 2
                elif card.title == 'Halfling':
                    if self.character_race == 4:
                        self.character_race = 3
                    else:
                        self.character_race2 = 3
                elif card.title == 'Half-Breed':
                    self.character_halfbreed = True

        # Update racial abilities
        if self.player == 1:
            # If dwarf, hand size = 6
            if self.character_race == 1 or self.character_race2 == 1:
                self.master_container[5].max_hand_size = 6
                self.max_big_items = 2
            else:
                self.master_container[5].max_hand_size = 5
                self.max_big_items = 1
            # If elf, +1 to run away and +1 to lvl for helping others
            if self.character_race == 2 or self.character_race2 == 2:
                self.to_run_away = [4,5,6]
            else:
                self.to_run_away = [5,6]
        elif self.player == 2:
            if self.character_race == 1 or self.character_race2 == 1:
                self.master_container[8].max_hand_size = 6
                self.max_big_items = 2
            else:
                self.master_container[8].max_hand_size = 5
                self.max_big_items = 1
            if self.character_race == 2 or self.character_race2 == 2:
                self.to_run_away = [4,5,6]
            else:
                self.to_run_away = [5,6]
        elif self.player == 3:
            if self.character_race == 1 or self.character_race2 == 1:
                self.master_container[11].max_hand_size = 6
                self.max_big_items = 2
            else:
                self.master_container[11].max_hand_size = 5
                self.max_big_items = 1
            if self.character_race == 2 or self.character_race2 == 2:
                self.to_run_away = [4,5,6]
            else:
                self.to_run_away = [5,6]
        elif self.player == 4:
            if self.character_race == 1 or self.character_race2 == 1:
                self.master_container[14].max_hand_size = 6
                self.max_big_items = 2
            else:
                self.master_container[14].max_hand_size = 5
                self.max_big_items = 1
            if self.character_race == 2 or self.character_race2 == 2:
                self.to_run_away = [4,5,6]
            else:
                self.to_run_away = [5,6]
        elif self.player == 5:
            if self.character_race == 1 or self.character_race2 == 1:
                self.master_container[17].max_hand_size = 6
                self.max_big_items = 2
            else:
                self.master_container[17].max_hand_size = 5
                self.max_big_items = 1
            if self.character_race == 2 or self.character_race2 == 2:
                self.to_run_away = [4,5,6]
            else:
                self.to_run_away = [5,6]
        elif self.player == 6:
            if self.character_race == 1 or self.character_race2 == 1:
                self.master_container[20].max_hand_size = 6
                self.max_big_items = 2
            else:
                self.master_container[20].max_hand_size = 5
                self.max_big_items = 1
            if self.character_race == 2 or self.character_race2 == 2:
                self.to_run_away = [4,5,6]
            else:
                self.to_run_away = [5,6]

